<?php


Class Magic {
    
    /**
     * типы заклинаний
    */
    static $all = array();
    static $type = array();
    
    function getType(){
        if(count(Magic::$type) > 0) return;
        Magic::$type['glade'] = array('timeLive', 'force', 'message', 'image');
        Magic::$type['drier'] = array('type', 'idelement', 'timeLive', 'force', 'message', 'image');
        Magic::$type['collection'] = array('type', 'idelement', 'timeLive', 'force', 'message', 'image');
        Magic::$type['mortar'] = array('count');
        Magic::$type['boiler'] = array('force');
        Magic::$type['invisible'] = array('timeLive', 'message', 'image');
        Magic::$type['education'] = array('count', 'level', 'message', 'image');
        Magic::$type['message'] = array('message', 'image', 'timeLive' );
        Magic::$type['privorot'] = array('message', 'image' );
        Magic::$type['moreforce'] = array('force', 'timeLive', 'message', 'image');
        Magic::$type['replace'] = array('timeLive', 'count', 'message', 'image');
        Magic::$type['bonuspotion'] = array('level', 'count', 'message', 'image');
        Magic::$type['practic'] = array('timeLive', 'force', 'message', 'image');
        Magic::$type['oracle'] = array('image');
    }
    
    static function getId($type){
        /*if($_SESSION["m_".$type]){
            return $_SESSION["m_".$type];    
        }*/  
        if(count(Magic::$all) == 0){
            Magic::getAll();
        }
        foreach(Magic::$all as $key => $val){
            if($val["name"] == $type) return $key;
            #$_SESSION["m_".$val["name"]] = $key;
        }
        #return $_SESSION["m_".$type];
    }
    static function getName($id){
        /*if($_SESSION["idm_".$id]){
            return $_SESSION["idm_".$id];    
        }*/  
        if(count(Magic::$all) == 0){
            Magic::getAll();
        }
        foreach(Magic::$all as $key => $val){
            if($key == $id) return $val['name'];
            #$_SESSION["idm_".$key] = $val["name"];
        }
        #return $_SESSION["idm_".$type];
    }
    static function getInfo($id){
        /*if($_SESSION["infom_".$id]){
            return $_SESSION["infom_".$id];    
        }  */
        if(count(Magic::$all) == 0){
            Magic::getAll();
        }
        foreach(Magic::$all as $key => $val){
            if($key == $id){
                return array('image'=> ( !Pet::is_url($val["image"]) ? MEDIA_URL.$val["image"] : $val["image"]  ) , 
                            'descript'=>$val["descript"]);
            }            
            #$_SESSION["infom_".$key] = array('image'=>$val["image"], 'descript'=>$val["descript"]);
        }
        #return $_SESSION["infom_".$type];
    }
    
    static function getAll($index = false){
        
        if( count(Magic::$all) > 0 ) {
            return true;
        }
        $sql = "SELECT * FROM `".PREFIX_STATIC."magic` ";
        if(!$result = DBConnect::query($sql, 'magic')){
            Connect::_error(mysql_error());
            return false;
        }
        while($arr = DBConnect::getOne($result, 'magic')){
            $arr['image'] = ( !Pet::is_url($arr['image']) ? MEDIA_URL.$arr['image'] : $arr['image']  ); 
            if($index != true){
                Magic::$all[$arr['id']] = $arr;    
            }else{
                Magic::$all[] = $arr;
            }            
        }
        return true;
    }
    
    static function getDescriptByName($mantra){
        Magic::getAll();
        #_echo( $mantraName );
        #_echo( Magic::$all );
        foreach(Magic::$all as $key => $val){
            if($val['name'] == $mantra){
                //_echo( $val['name']." = ".$mantra['mantra'] );
                #return $val;
                $mantra['id'] = $val['id'] ;
                return $val['descript']; 
                #return Magic::getDescript($mantra);
            }else{
                #return " ";
            }
        }
    }
    
    static function getImageByName($mantra){
        Magic::getAll();
        #_echo( $mantraName );
        #_echo( Magic::$all );
        foreach(Magic::$all as $key => $val){
            if($val['name'] == $mantra){
                _echo( $val['name']." = ".$mantra['mantra']."  ".$val["image"] );                
                return $val["image"];
            }else{
                #return " ";
            }
        }
    }        
    
    static function getDescript($mantra){        
        Magic::getAll(false);
        $arr = array();
        switch($mantra['id']){
            case 1 :
                $arr['doit'] = 'хабар';
            break;                
            
            case 2 :
                switch ($mantra['element']){
                    case "habar":
                        Habars::getHabars();
                        $arr['doit'] = Habars::$habars[$mantra['idelement']]['name'];
                    break;
                    case "elementhabar":
                        Habars::getElement($mantra['idelement']);
                        $arr['doit'] = Habars::$elements[$mantra['idelement']]['name'];
                    break;
                }
            break;
            
            case 3 :
                switch ($mantra['element']){
                    case "habar":
                        Habars::getHabars();
                        $arr['doit'] = Habars::$habars[$mantra['idelement']]['name'];
                    break;
                    case "elements":
                        Habars::getElement($mantra['idelement']);
                        $arr['doit'] = Habars::$elements[$mantra['idelement']]['name'];
                    break;
                }
            break;
            
            case 4 :
            break;
            
            case 5 :
            break;
                        
        }
        $arr['force'] = $mantra['force'];
        $arr['timelive'] = $mantra['timelive'];
        $arr['descript'] = Magic::$all[$mantra['id']]['descript'];
        return $arr;
    }
    
    static function splitDescript($arr){
        $desc = "Заклинание ".$arr['descript'].". ";
        if( $arr['doit'] ){
            $desc .= " Действие: ".$arr['doit']  ;
        }
        if( floatval($arr['force']) != 0){
            $desc .= "  сила действия ".$arr['force']. " % ";
        }
        if( floatval($arr['timelive']) != 0){
            $desc .= "  время действия: до ".strftime("%d.%m.%Y %H:%M:%S ", time() + $arr['timelive']). " ";
            #$desc .= "  время действия: до ".$arr['timelive']. " ";
        }
        return $desc;
    }  
    
    function getOracle(){
        $sql = "SELECT * FROM `".PREFIX_STATIC."oracle`";
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        DBConnect::getAll($result); 
        return DBConnect::$all;
    }  
    
    function saveOracle($data){
        $sql = "UPDATE `".PREFIX_STATIC."oracle` 
                SET `text` = '".$data['text']."' 
                WHERE `id` = '".$data['id']."' ";
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        return true;
    }
    
    function addOracle($data){
        $sql = "INSERT INTO `".PREFIX_STATIC."oracle` (`text`)
                VALUES ('".$data['text']."') ";
        #return $sql;
        #_echo($sql);
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        return true;
    }
    
    function delOracle($id){
        $sql = "DELETE FROM `".PREFIX_STATIC."oracle` WHERE `id` = '".$id."' ";
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        return true;
    }
}
?>